define(['./../../initial'],
    function() {
        var app = angular.module('App.home');
        app.controller('admin.project.configCtrl', ['$timeout', '$scope', '$rootScope', '$state', '$stateParams', 'User', 'showMsg', 'Project', 'authorizedUsers', 'cityList', 'userCorps', 'curPrj',
            function($timeout, $scope, $rootScope, $state, $stateParams, User, showMsg, Project, authorizedUsers, cityList, userCorps, curPrj) {

                $scope.userCorps = userCorps;
                $scope.authorizedUsers = authorizedUsers;

                //初始化popup,提示管理员无法修改
                $timeout(function() {
                    $('.real-name.disabled').popup({
                        on: 'hover'
                    });
                }, 100);

                function checkUserList() {
                    userCorps.forEach(function(corp) {
                        corp.departmentList.forEach(function(depart) {
                            depart.userList.forEach(function(user) {
                                if (user.authorizedProjectList.indexOf(curPrj.prjId) >= 0) {
                                    user.checked = true;
                                    user.backupChecked = true; //再设置一个临时的备份
                                } else {
                                    user.checked = false;
                                    user.backupChecked = false;
                                }
                            });
                        });
                    });
                }
                checkUserList();

                $scope.toggleUserCheck = function(user) {
                    if (user.hasAdminPower()) return;
                    user.checked = !user.checked;
                    //设置用户为已更新
                    user.modified = true;
                    debugger;
                };

                $scope.updateUserAuthorizedProjectList = function() {
                    var userPrjList = $scope.authorizedUsers.filter(function(user) {
                        return user.modified === true && user.backupChecked !== user.checked;
                    }).map(function(user) {
                        return {
                            userName: user.userName,
                            prjId: curPrj.prjId,
                            remark: user.checked.toString()
                        };
                    });
                    var u = new User();
                    u.postUpdateUserAuthorizedProjectList(userPrjList).then(function(data) {
                        if (data.toString() === 'true') {
                            showMsg('修改成功!', 'info');
                            applyChange();
                        } else {
                            showMsg('发生错误!', 'alert');
                        }
                    });
                };

                function applyChange() {
                    authorizedUsers.forEach(function(user) {
                        if (user.modified === true && user.backupChecked !== user.checked) {
                            if (user.checked) { //并且列表里还没有
                                user.authorizedProjectList.push(curPrj.prjId);
                            } else { //并且列表里还有
                                var index = user.authorizedProjectList.indexOf(curPrj.prjId);
                                user.authorizedProjectList.splice(index, 1);
                            }
                        }
                    });
                }
            }
        ]);
    });
